<?php

/**
 * 
 * 导入文件
 * @param string $path
 * @param bool or string $ext
 * @param string $baseUrl
 */
function import($path, $ext = true, $baseUrl = COM_DIR) {
    $file = $baseUrl . __DS__ . str_replace('.', __DS__, $path);
    $file .= (is_bool($ext) ? ($ext ? '.class.php' : '.php') : $ext);
    static $f_cache = array();
    $env_name = "IMPORT_{$baseUrl}_{$path}";
    if (!isset($f_cache[$env_name])) {
        if (is_file($file)) {
            $f_cache[$env_name] = require ($file);
        } else {
            return false;
        }
    }
    return $f_cache[$env_name];
}

/**
 * 获取系统配置文件变量
 */
function getCfgVar($varname) {
    static $config = null;
    if (!$config) {
        $config = import('config.sysinfo', false, app::$APP_ROOTDIR); //项目的根目录
    }
    return $config[$varname];
}

function _get($key) {
    $value = isset($_GET[$key]) ? $_GET[$key] : '';
    $value = trim($value);
    return($value);
}

function _post($key) {
    $value = isset($_POST[$key]) ? $_POST[$key] : '';
    $value = trim($value);
    return($value);
}

function print_rr($array) {
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}

function getExistsFileByArr($files) {
    //获取存在的文件在数组里
    foreach ($files as $file) {
        if (file_exists($file)) {
            return($file);
        }
    }
    return('');
}

function rtsp_exists($url) {
    $server = parse_url($url, PHP_URL_HOST);
    $port = "554";
    $hdrs = "DESCRIBE " . $url . " RTSP/1.0" . "\r\n\r\n";
    //Open connection (15s timeout)
    $sh = fsockopen($server, $port, $err, $err_otp, 15);
    //Check connections
    if (!$sh)
        return false;
    //Send headers
    fputs($sh, $hdrs);
    //Receive data (1KB)
    $rtds = fgets($sh, 1024);
    //Close socket
    fclose($sh);
    return strpos($rtds, "200 OK") > 0;
}

/*
 * function strpos_int()
 * PHP在字符串中查找某字符串第N次出现的起始位置
 */

function strpos_int($string, $find, $i) {
    $pos = -1;
    $n = 0;
    do {
        $pos = strpos($string, $find, $pos + 1);
        $n++;
        if ($n == $i) {
            return($pos);
        }
    } while ($pos !== false);
    return(false);
}

/*
 * XML生成
 */

function xml_encode($arr, $dom = 0, $item = 0) {
    if (!$dom) {
        $dom = new DOMDocument('1.0', 'utf-8');
    }
    if (!$item) {
        $item = $dom->createElement('root');
        $dom->appendChild($item);
    }
    foreach ($arr as $key => $val) {
        $itemx = $dom->createElement(is_string($key) ? $key : 'item');
        $item->appendChild($itemx);
        if (!is_array($val)) {
            if (0 && strIsBin($val)) {
                $text = $dom->createCDATASection($val);
            } else {
                $text = $dom->createTextNode($val);
            }
            $itemx->appendChild($text);
        } else {
            xml_encode($val, $dom, $itemx);
        }
    }
    return $dom->saveXML();
}

function str(&$var) {
    return(isset($var) ? trim($var) : '');
}

function arr(&$var) {
    return((isset($var) && is_array($var)) ? $var : array());
}